package com.dongbo.recorder.screenrecord.utils;

import static android.content.pm.PackageManager.GET_UNINSTALLED_PACKAGES;

import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * author:lutengxuan
 * date:2022/12/12
 * describe:判断程序运行，防止页面拦截
 */
public class AntiHiJack {

    public static final String TAG = "AntiHiJack";

    // white name list
    private static List<String> safePackages;

    static {
        safePackages = new ArrayList<>();
    }

    /**
     * 配置白名单
     */
    public static void configSafePackages(List<String> packages) {
        safePackages = packages;
    }

    private static PackageManager pm;
    private List<ApplicationInfo> mlistAppInfo;

    /***
     *    检测当前Activity是否安全
     */
    public static boolean checkActivity(Context context) {
        boolean safe = false;
        pm = context.getPackageManager();

        if (safePackages == null || safePackages.size() <= 0) {
            int flag = GET_UNINSTALLED_PACKAGES;//7.0之后被遗弃
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                flag = PackageManager.MATCH_UNINSTALLED_PACKAGES;
            }
//         查询所有已经安装的应用程序
            List<ApplicationInfo> listAppcations =
                    pm.getInstalledApplications(flag);
            Collections.sort(listAppcations, new ApplicationInfo.DisplayNameComparator(pm));// 排序
            //List<ApplicationInfo> appInfos = new ArrayList<ApplicationInfo>(); // 保存过滤查到的AppInfo
            //appInfos.clear();
            for (ApplicationInfo app : listAppcations) {//这个排序必须有.
                //得到所有的系统程序包名放进白名单里面.
                if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                    //appInfos.add(getAppInfo(app));
                    safePackages.add(app.packageName);
                }
            }
        }else {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        String runningActivityPackageName;
        int sdkVersion;

        try {
            sdkVersion = Build.VERSION.SDK_INT;
        } catch (NumberFormatException e) {
            sdkVersion = 0;
        }

        if (sdkVersion >= 21) {
            //获取系统api版本号,如果是5x系统就用这个方法获取当前运行的包名
            runningActivityPackageName = getCurrentPkgName(context);
        } else {
            //如果是4x及以下,用这个方法.
            runningActivityPackageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
        }
        if (runningActivityPackageName != null) {//有些情况下在5x的手机中可能获取不到当前运行的包名，所以要非空判断。
//            Log.e("TAG__", "currentPackage:" + runningActivityPackageName + "  当前应用的包名：" + context.getPackageName());
            if (runningActivityPackageName.equals(context.getPackageName())) {
                safe = true;
            }

            // 白名单比对
            for (String safePack : safePackages) {
                Log.e("safePack", safePack);
                if (safePack.equals(runningActivityPackageName)) {
                    safe = true;
                }
            }
        }

        return safe;
    }

    public static String getCurrentPkgName(Context context) {//5x系统以后利用反射获取当前栈顶activity的包名.
        ActivityManager.RunningAppProcessInfo currentInfo = null;
        Field field = null;
        int START_TASK_TO_FRONT = 2;
        String pkgName = null;

        try {
            field = ActivityManager.RunningAppProcessInfo.class.getDeclaredField(
                    "processState");//通过反射获取进程状态字段.
        } catch (Exception e) {
            e.printStackTrace();
        }
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List appList = am.getRunningAppProcesses();
        ActivityManager.RunningAppProcessInfo app;

        for (int i = 0; i < appList.size(); i++) {
            //ActivityManager.RunningAppProcessInfo app : appList
            app = (ActivityManager.RunningAppProcessInfo) appList.get(i);
            if (app.importance
                    == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {//表示前台运行进程.
                Integer state = null;
                try {
                    state = field.getInt(app);//反射调用字段值的方法,获取该进程的状态.
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (state != null && state == START_TASK_TO_FRONT) {//根据这个判断条件从前台中获取当前切换的进程对象.
                    currentInfo = app;
                    break;
                }
            }
        }

        if (currentInfo != null) {
            pkgName = currentInfo.processName;
        }
        return pkgName;
    }

    /***
     *   * 当前停留在desktop*
     *   **
     *   ****@param***context 上下文*
     *   */
    public static boolean isHome(Context context) {
        ActivityManager mActivityManager =
                (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
        return getHomes(context).contains(rti.get(0).topActivity.getPackageName());
    }

    /***
     *   * 获得属于desktop应用的应用包名称*
     *   **
     *   ****@return***返回包含所有包名的字符串列表*
     *   */
    private static List<String> getHomes(Context context) {
        List<String> names = new ArrayList<>();
        PackageManager packageManager = context.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo ri : resolveInfo) {
            names.add(ri.activityInfo.packageName);
        }
        return names;
    }

    /***
     *   * 判断当前是否在锁屏再解锁状态*
     *   **
     *   ****@param***context 上下文*
     *   */
    public static boolean isReflectScreen(Context context) {
        KeyguardManager mKeyguardManager =
                (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        return mKeyguardManager.inKeyguardRestrictedInputMode();
    }

}
